/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.phone.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.phone.entity.Users;
import com.jeesite.modules.phone.service.UsersService;

/**
 * 用户表Controller
 * @author lzw
 * @version 2019-04-03
 */
@Controller
@RequestMapping(value = "${adminPath}/phone/users")
public class UsersController extends BaseController {

	@Autowired
	private UsersService usersService;
	
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public Users get(Long id, boolean isNewRecord) {
		return usersService.get(id+"", isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("phone:users:view")
	@RequestMapping(value = {"list", ""})
	public String list(Users users, Model model) {
		model.addAttribute("users", users);
		return "modules/phone/usersList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("phone:users:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<Users> listData(Users users, HttpServletRequest request, HttpServletResponse response) {
		Page<Users> page = usersService.findPage(new Page<Users>(request, response), users); 
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("phone:users:view")
	@RequestMapping(value = "form")
	public String form(Users users, Model model) {
		model.addAttribute("users", users);
		return "modules/phone/usersForm";
	}

	/**
	 * 保存用户表
	 */
	@RequiresPermissions("phone:users:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated Users users) {
		usersService.save(users);
		return renderResult(Global.TRUE, "保存用户表成功！");
	}
	
	/**
	 * 删除用户表
	 */
	@RequiresPermissions("phone:users:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(Users users) {
		usersService.delete(users);
		return renderResult(Global.TRUE, "删除用户表成功！");
	}
	
}